package com.lecture.java8.excel.a0327;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;

import java.util.ArrayList;
import java.util.List;

public class ExcelMergeTest {

    public static void main(String[] args) {
        String fileName = "D:/test.xlsx";

        List<DataModel> dataList = new ArrayList<>();
        dataList.add(new DataModel("A", "X"));
        dataList.add(new DataModel("A", "Y"));
        dataList.add(new DataModel("B", "Z"));
        dataList.add(new DataModel("B", "Z"));
        dataList.add(new DataModel("B", "Z"));

        ExcelWriterBuilder writerBuilder = EasyExcel.write(fileName, DataModel.class);
        ExcelWriterSheetBuilder sheetBuilder = writerBuilder.sheet("Sheet1");
        sheetBuilder.registerWriteHandler(new MergeSameColumnStrategy(0)); // 0 表示合并第一列
        sheetBuilder.doWrite(dataList);
    }

    public static class DataModel {
        private String col1;
        private String col2;

        public DataModel(String col1, String col2) {
            this.col1 = col1;
            this.col2 = col2;
        }

        public String getCol1() {
            return col1;
        }

        public void setCol1(String col1) {
            this.col1 = col1;
        }

        public String getCol2() {
            return col2;
        }

        public void setCol2(String col2) {
            this.col2 = col2;
        }
    }
}
